﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hrm.Core;
using AutoMapper;
using AutoMapper.QueryableExtensions;

namespace Hrm.Services
{
    public class UserService : BaseService<User>, IUserService
    {
        private readonly IUserPermissionDbRepository _personRepository;

        public UserService(IUserPermissionDbRepository personRepository)
            : base(personRepository)
        {
            this._personRepository = personRepository;
        }

        public UserDto GetPerson(string userCode, string password)
        {
            return _personRepository.Table.Where(p => (p.Code == userCode && p.Password == password && p.IsEnable == true)).Project().To<UserDto>().SingleOrDefault();
        }


        public PageResponse<UserDto> GetPageList(PageRequest<UserDto> request)
        {
            var query = this.GetAllAbledAndOrderByCreatetime();
            if (!string.IsNullOrEmpty(request.filter.Name))
            {
                query = query.Where(p => p.Name.Contains(request.filter.Name));
            }

            if (!string.IsNullOrEmpty(request.filter.Code))
            {
                query = query.Where(p => p.Code == request.filter.Code);
            }
            var data = query.Project().To<UserDto>();

            PageResponse<UserDto> result = new PageResponse<UserDto>(data, request.draw, request.start, request.length);
            return result;
        }


        public User GetPerson(string userCode)
        {
            return this._personRepository.GetAll().FirstOrDefault(p => p.Code == userCode);
        }
    }
}
